﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Travel.CommonUtil.Snowflake;
using Travel.Data;

namespace Travel.Service
{
    /// <summary>
    /// 基础服务
    /// </summary>
    public class ServiceBase
    {
        /// <summary>
        /// 当前登录人ID
        /// </summary>
        protected long LoginUserId { get; set; }
        /// <summary>
        /// 数据库上下文
        /// </summary>
        protected HappyTravelContext TravelContext => LocalSericeProvider.Instance.GetService<HappyTravelContext>();
        /// <summary>
        /// AutoMap 实例
        /// </summary>
        protected IMapper ObjectMapper => LocalSericeProvider.Instance.GetService<IMapper>();
        /// <summary>
        /// 雪花ID 实例
        /// </summary>
        protected IdWorker SnowIdWorker => SnowflakeUtil.CreateIdWorker();

        public ServiceBase() {
          var httpContextAccessor =  LocalSericeProvider.Instance.GetService<IHttpContextAccessor>();
            if (httpContextAccessor?.HttpContext?.User.Identity is { IsAuthenticated: true })
            {
                LoginUserId = Convert.ToInt64(httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(
                p => p.Type.Equals("UserId"))!.Value);
            }
        }
    }
}
